package com.xxx.sso.config;

import com.xxx.sso.consts.RedisConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author caiwl
 * @date 2020/6/11 9:27
 */
@Component
public class RedisService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 设置key值和过期时间
     * @param key
     * @param value
     */
    public void set(String key, String value) {
        stringRedisTemplate.opsForValue().set(key, value, RedisConst.TIMEOUT, TimeUnit.MINUTES);
    }

    /**
     * 获取key值
     * @param key
     * @return
     */
    public String get(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    /**
     * 设置过期时间
     * @param key
     */
    public void expire(String key) {
        stringRedisTemplate.expire(key, RedisConst.TIMEOUT, TimeUnit.MINUTES);
    }

    /**
     * 删除指定key
     * @param key
     * @return
     */
    public boolean delete(String key) {
        return stringRedisTemplate.delete(key);
    }
}
